﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WeigherMoudel
{

    public class AndWeigher : Weigher
    {
        public AndWeigher(string name, bool isShowForm) : base(name, isShowForm)
        {

        }

        public override string ZeroCMD => "R\r\n";

        public override string PowerOFFCMD => "OFF\r\n";

        public override string PowerONCMD => "ON\r\n";


        private  RetWeight ConvertString(string content)
        {
            if (string.IsNullOrEmpty(content))
                return null;
            string ret = "";
            if (content.Contains("ST,"))
            {
                char[] chenzhong = content.Split(',')[1].ToCharArray(0, 9);
                for (int i = 0; i < 9; i++)
                {
                    ret += chenzhong[i];
                }
                return new RetWeight(true, ret);

            }
            else if (content.Contains("US,"))
            {
                char[] chenzhong = content.Split(',')[1].ToCharArray(0, 9);
                for (int i = 0; i < 9; i++)
                {
                    ret += chenzhong[i];
                }
                return new RetWeight(false, ret);
            }
            else
            {
                return null;
            }
        }

        internal override void Weightserial_OnReceiveData(string obj)
        {
            if (obj.Contains("@RE"))
                return;
            lastupdatetime = DateTime.Now;
            if (issendClear)  //正在发送清零指令的时候不接受
                return;
            ContempWeight = ConvertString(obj);
            base.Weightserial_OnReceiveData(obj);
        }
    }
}
